package com.luxoft.scoremanager.server.dao.hibernateimpl;

import com.luxoft.scoremanager.server.dao.ComponentDao;
import com.luxoft.scoremanager.server.dao.ComponentValuesDao;
import com.luxoft.scoremanager.server.dao.ScorePackageDao;
import com.luxoft.scoremanager.server.dao.StaticDataDao;
import com.luxoft.scoremanager.server.dao.exception.HibernateRecordException;
import com.luxoft.scoremanager.server.dao.hibernateimpl.util.HibernateUtil;
import com.luxoft.scoremanager.server.model.dao.ComponentDaoModel;
import com.luxoft.scoremanager.server.model.dao.ComponentValuesDaoModel;
import com.luxoft.scoremanager.server.model.dao.ScorePackageDaoModel;
import com.luxoft.scoremanager.server.model.dao.StaticDataDaoModel;
import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;


public class HibernateScorePackageDao implements ScorePackageDao {
    @Autowired
    private static Logger log = Logger.getLogger(HibernateScorePackageDao.class);

    public HibernateScorePackageDao() {

    }

    private void initData() {

        ComponentDao daoComponent = new HibernateComponentDao();
        ComponentValuesDao daoComponentValues = new HibernateComponentValuesDao();
        StaticDataDao daoStaticData = new HibernateStaticDataDao();

        daoStaticData.addStaticData(new StaticDataDaoModel("Very weak", "1"));
        daoStaticData.addStaticData(new StaticDataDaoModel("Weak", "1"));
        daoStaticData.addStaticData(new StaticDataDaoModel("Adequate", "1"));
        daoStaticData.addStaticData(new StaticDataDaoModel("Suspicious", "1"));
        daoStaticData.addStaticData(new StaticDataDaoModel("Moderate", "1"));
        daoStaticData.addStaticData(new StaticDataDaoModel("Very weak", "2"));
        daoStaticData.addStaticData(new StaticDataDaoModel("Weak", "2"));
        daoStaticData.addStaticData(new StaticDataDaoModel("Adequate", "2"));

        ScorePackageDaoModel scorePackage1 = new ScorePackageDaoModel("IN_COMMITTEE");
        ComponentDaoModel component1 = new ComponentDaoModel("Risk position with Extraordinary Support", "1");
        ComponentDaoModel component2 = new ComponentDaoModel("Growth and Changes in Exposur", "1");
        ComponentDaoModel component3 = new ComponentDaoModel("Risk Concentration and Risk Diversification", "1");
        ComponentDaoModel component4 = new ComponentDaoModel("Complexity", "1");
        ComponentDaoModel component5 = new ComponentDaoModel("Risks not covered by RACF", "2");
        ComponentDaoModel component6 = new ComponentDaoModel("Evidence of stronger or weak Loss Experience", "1");
        daoComponent.addComponent(component1);
        daoComponent.addComponent(component2);
        daoComponent.addComponent(component3);
        daoComponent.addComponent(component4);
        daoComponent.addComponent(component5);
        daoComponent.addComponent(component6);

        scorePackage1.addComponentValues(new ComponentValuesDaoModel("Very weak", "Weak", component1, scorePackage1));
        scorePackage1.addComponentValues(new ComponentValuesDaoModel("Weak", "Adequate", component2, scorePackage1));
        scorePackage1.addComponentValues(new ComponentValuesDaoModel("Adequate", "Very weak", component3, scorePackage1));
        scorePackage1.addComponentValues(new ComponentValuesDaoModel("Suspicious", "Adequate", component4, scorePackage1));
        scorePackage1.addComponentValues(new ComponentValuesDaoModel("Very weak", "Adequate", component5, scorePackage1));

        ScorePackageDaoModel scorePackage2 = new ScorePackageDaoModel("PRE_COMMITTEE");
        scorePackage2.addComponentValues(new ComponentValuesDaoModel("Weak", "Weak", component1, scorePackage2));
        scorePackage2.addComponentValues(new ComponentValuesDaoModel("Adequate", "Moderate", component2, scorePackage2));
        scorePackage2.addComponentValues(new ComponentValuesDaoModel("Adequate", "Weak", component6, scorePackage2));

        this.addPackage(scorePackage1);

        this.addPackage(scorePackage2);

        log.info("InitData success");
    }

    @Override
    public void addPackage(ScorePackageDaoModel scorePackage) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.saveOrUpdate(scorePackage);
            session.getTransaction().commit();
        } catch (Exception e) {

            log.info("Error score package save");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void updatePackage(ScorePackageDaoModel scorePackage) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(scorePackage);
            session.getTransaction().commit();
        } catch (Exception e) {

            log.info("Error score package update");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    @Override
    public void deletePackage(ScorePackageDaoModel scorePackage) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(scorePackage);
            session.getTransaction().commit();
        } catch (Exception e) {
            log.info("Error component delete");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {

                session.close();
            }
        }
    }

    @Override
    public List<ScorePackageDaoModel> getAll() {
        Session session = null;
        List scorePackageDaoModelList = new ArrayList<ScorePackageDaoModel>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            scorePackageDaoModelList = session.createCriteria(ScorePackageDaoModel.class).list();
        } catch (Exception e) {

            log.info("Error score package get all");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return scorePackageDaoModelList;
    }

    @Override
    public ScorePackageDaoModel getById(int id) {
        Session session = null;
        ScorePackageDaoModel scorePackage = new ScorePackageDaoModel();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            scorePackage = (ScorePackageDaoModel) session.get(ScorePackageDaoModel.class, id);

        } catch (Exception e) {

            log.info("Error component get by id");
            throw new HibernateRecordException(e.getMessage());
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return scorePackage;

    }
}
